<?
class myDB {
	protected $dbLink;
	public $debug=false, $showQuery=false;

	/**
	 * Shows debug alerts from the mysql server
	 */
	public function debug(){ header('Content-Type: text/html; charset=utf-8'); return $this->debug=true; }

	/**
	 * Displays sql query before execution
	 */
	public function showQuery(){ header('Content-Type: text/html; charset=utf-8'); return $this->showQuery=true; }
	
	/**
	 * Retrieves record by id
	 * @param String $table		table name
	 * @param Number $id			id of the record
	 */
	public function get_by_id($table,$id){ return $this->dbObject ('SELECT * FROM '.$table.' WHERE id='.intval($id)); }

	/**
	 * Retrieves an array of records
	 * @param String $table		table name
	 * @param String $sort		optional column name to sort records
	 */
	public function get_all($table, $sort=''){ return $this->dbTable ('SELECT * FROM '.$table.(!empty($sort)?' ORDER BY '.$sort:'')); }
	
	/**
	 * Removes record from the DB
	 * @param String $table		table name
	 * @param Number $id			id of the record to remove
	 */
	public function delData($table,$id){ return $this->dbEdit('DELETE FROM '.$table.' WHERE id='.intval($id)); }
	
	/**
	 * Adds record to the DB
	 * @param String $table		table name
	 * @param Array $data		record data (with $array['id'])
	 * @param String $fields	a comma separated list of field names to loop through
	 * @return Number				id of the new record
	 */
	public function addData($table,$data,$fields){ // global editing function, i.e.: add_data('clients',$data_array,'school_id,title,name,surname');
		$field_ar=split(',',$fields);	$sql='';
		if (!empty($field_ar)) foreach ($field_ar as $field)	$sql.='"'.$data[$this->clrStr($field)].'",';
		$sql=trim($sql,',');
		$this->dbEdit('INSERT INTO '.$table.' ('.$fields.') VALUES ('.$sql.')');
		return mysql_insert_id();
	}

	/**
	 * Edits a record in a DB
	 * @param String $table		table name
	 * @param Array $data		record data (with $array['id'])
	 * @param String $fields	a comma separated list of field names to loop through
	 * @return 						mysql_query result
	 */
	public function editData($table,$data,$fields){
		$field_ar=split(',',$fields);	$sql='';
		if (!empty($field_ar)) foreach ($field_ar as $field)	$sql.=' '.$field.'="'.$data[$this->clrStr($field)].'",';
		$sql=trim($sql,',');
		return $this->dbEdit('UPDATE '.$table.' SET '.$sql.' WHERE id="'.$data[id].'"');
	}
	
	/**
	 * Returns an associative array for one record
	 * @param String $sql	sql query
	 * @return Array	an associative array
	 */
	public function dbObject ($sql){
		if ($this->showQuery) echo '<br><code><b>'.$sql.'</b></code><br><br>';
		if ($this->debug==true) $query = mysql_query($sql) or print('Cannot process the query: '.$sql.'<br>'.mysql_error());
		else $query = @mysql_query($sql) or print('Database Error.');
		if (!empty($query)) $fecz = mysql_fetch_assoc($query);
		return $fecz;
	}

	/**
	 * Returns a multidimensional array of results
	 * @param String $sql	sql query
	 * @return Array	multidimensional associative array of results
	 */
	public function dbTable ($sql){
		if ($this->showQuery) echo '<br><code><b>'.$sql.'</b></code><br><br>';
		if ($this->debug==true) $query = mysql_query($sql) or print('Cannot process the query: '.$sql.'<br>'.mysql_error());
		else $query = @mysql_query($sql) or print('Database Error.');
		$tab=array();
		if ($query) while ($fecz = mysql_fetch_assoc($query))  $tab[]=$fecz;
		return $tab;
	}
	
	/**
	 * Straight sql edit
	 * @param String $sql	sql query 
	 */
	public function dbEdit ($sql){
		if ($this->showQuery) echo '<br><code><b>'.$sql.'</b></code><br><br>';
		if ($this->debug==true) $query = mysql_query($sql) or print('Cannot process the query: '.$sql.'<br>'.mysql_error());
		else $query = mysql_query($sql);
		return $query; 
	}

	protected function clrStr($str){
		$str=str_replace(array('(',')',';','"',"'","\\",'`'), array('','','','','','',''), $str); 
		return trim($str); 
	}
	
	function __construct ($db, $host='localhost', $user='root', $pass=''){
		$this->dbLink=@mysql_connect($host, $user, $pass);// or die('Connection error: '.mysql_error());
		@mysql_select_db($db);// or die ('Error selecting DB!'); 
		@mysql_query('SET NAMES utf8');// or die ('Error setting code page');
	}
	function __destruct (){ @mysql_close($this->dbLink); }	
}
?>